//
//  PYImageManager.m
//  IHome
//
//   Created by zhujizheng on 2020/2/18.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYImageManager.h"

@implementation PYImageManager

/**
 获取图片在硬盘里的缓存大小
 
 @param block 格式化后的空间大小字符串，已经在主线程中回调了
 */
+ (void)getImageStoreSpaceWithFormatString:(void (^)(NSString * _Nonnull))block
{
    if (block == nil) {
         NSLog(@"block不能为空");
        return;
    }
    [[YYImageCache sharedCache].diskCache totalCostWithBlock:^(NSInteger totalCost) {
        dispatch_async_on_main_queue(^{
            NSInteger number = totalCost;
            float space = 1;
            NSArray *arr = @[@"Bytes", @"K", @"M", @"G"];
            for (int i = 0; i < arr.count; i++) {
                space *= 1024.f;
                if (number < space) {
                    float value = number * 1024.f / space;
                    if (value == 0) {
                        block(@"无缓存");
                    } else if (value < 1024) {
                        block([NSString stringWithFormat:@"%d %@", (int)value, arr[i]]);
                    } else {
                        block([NSString stringWithFormat:@"%0.1f %@", value, arr[i]]);
                    }
                    break;
                }
            }
        });
    }];
}
/**
 清除硬盘所有图片
 
 @param completion 清除完成的回调，已经在主线程回调了
 */
+ (void)clearAllDiskImage:(void(^)(BOOL))completion
{
    [[[YYImageCache sharedCache] diskCache] removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {
        
    } endBlock:^(BOOL error) {
        dispatch_async_on_main_queue(^{
            if (completion) {
                completion(error);
            }
        });
    }];
}

/**
 清除所有图片，包括硬盘和内存中的图片
 */
+ (void)clearAllImage
{
    [[[YYImageCache sharedCache] diskCache] removeAllObjects];
    [[[YYImageCache sharedCache] memoryCache] removeAllObjects];
}

@end
